﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMM.Tapioca.Instance;
using WMM.Tapioca.Taxonomy;
using WMM.Tapioca.Linkbase;

namespace WMM.Tapioca.Formula.Filters
{
    /// <summary>
    /// The unit matching filter can be used to select facts that have the same unit. 
    /// </summary>
    public class MatchUnitFilter : MatchFilter
    {
        /// <summary>
        /// Constructor.
        /// </summary>
        public MatchUnitFilter(Resource resource)
            : base(resource)
        {
            
        }


        public override List<FactItem> EvaluateFilter(VariableSet variableSet, List<FactItem> itemsToFilter, DTS dts)
        {
            this.dts = dts;
            namespaces = dts.DtsFormula.Namespaces;

            List<FactItem> itemListTemp = new List<FactItem>();

            foreach (FactItem itemToFilter in itemsToFilter)
            {
                List<FactItem> factItems = variableSet.variableItems[Variable];

                for (int i = 0; i < factItems.Count; i++)
                {
                    if (Complement ? !itemToFilter.UnitEquals(factItems[i]) : itemToFilter.UnitEquals(factItems[i]))
                    {
                        itemListTemp.Add(itemToFilter);
                        continue;
                    }
                }
            }

            return itemListTemp;
        }
    }
}
